package com.union.core.workModel.action;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.workModel.model.ModelBonus;
import com.union.core.workModel.model.ModelIncom;
import com.union.core.workModel.model.ModelInfo;
import com.union.core.workModel.model.ModelMember;
import com.union.core.workModel.service.ModelInfoService;

public class ModelAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//登录
	private String LOGIN_NAME;
	private String LOGIN_PWD;
	private String operaResult;
	//劳模列表
	private List<ModelInfo> modelList;
	//劳模对象
	private ModelInfo modelInfo;
	private final String SAVEDIR="/upload/model";
	//分页
	private int start;
	private int intPageSize; //每页行数
	private int page = 1;      //页码
	private int intRowCount;   //总行数
	private int intPageCount;  //总页数
	private int intPage;       //页数变量
	//查询条件参数
	private int sex;
	//private String honour; //客户表示不用这个字段做筛选了
	private String level;
	private String current;
	private String paramName;
	private String paramValue;
	private String orderByName;
	private ModelInfoService modelInfoService;
	//导出目标id
	private int[] ids;
	//劳模收入
	private ModelIncom modelIncom;
	private Float[] incoms;
	private String[] incomyears;
	//劳模补助
	private Float[] buzhus;
	private String[] buzhuyears;
	//家庭成员
	private ModelMember modelMember;
	private String[] fname;
	private int[] fsex;
	private String[] fbirth;
	private String[] relation;
	private String[] fjob;
	private Float[] fincom;
	//劳模援助金
	private List<ModelBonus> modelBonusList;
	public ModelAction(){
		modelInfoService=new ModelInfoService();
	}
	public String goLogin(){
		return "goLogin";
	}
	public String doLogin(){
		if(!"".equals(LOGIN_NAME) && !"".equals(LOGIN_PWD)){
			if(LOGIN_NAME.equals("admin")){
				if(LOGIN_PWD.equals("model@Admin")){
					return goModelList();
				}else{
					operaResult="密码错误！";
					return "goLogin";
				}
			}else{
				operaResult="用户名不存在！";
				return "goLogin";
			}
		}else{
			operaResult="用户名密码不能为空！";
			return "goLogin";
		}
	}
	//跳转：导出模板页面
	public String goOutput(){
		return "goOutput";
	}
	//跳转：添加劳模页面
	public String goAddModel(){
		return "goAddModel";
	}
	//跳转：劳模详细页面
	public String goModelDetail(){
		return "goModelDetail";
	}
	//跳转：所有劳模援助金页面
	public String goAllBonus(){
		return "goAllBonus";
	}
	//跳转：单个劳模援助金
	public String goModelBonus(){
		int mid=modelInfo.getModel_id();
		modelBonusList=modelInfoService.queryBonusList(mid);
		return "goModelBonus";
	}
	//添加：添加劳模信息
	public String doAddModel(){
		if(modelInfo.getFileupload()!=null){
			String new_image=picfileupload(111);
			modelInfo.setModel_image(new_image);
		}
		
		int iid=modelInfoService.addModel(modelInfo);
		if(iid!=0){
			modelIncom=new ModelIncom();
			modelMember=new ModelMember();
			//收入记录
			for(int i=0;i<incoms.length;i++){
				if(!"".equals(incomyears[i])&& null!=incoms[i]){
					modelIncom.setIncome_year(incomyears[i]);
					modelIncom.setIncome_sum(incoms[i]);
					modelIncom.setModel_id(iid);
					modelIncom.setIncome_type(1);
					modelInfoService.addIncom(modelIncom);
				}
			}
			//补助记录
			for(int j=0;j<buzhus.length;j++){
				if(!"".equals(buzhuyears[j])&& null!=buzhus[j]){
					modelIncom.setIncome_year(buzhuyears[j]);
					modelIncom.setIncome_sum(buzhus[j]);
					modelIncom.setModel_id(iid);
					modelIncom.setIncome_type(2);
					modelInfoService.addIncom(modelIncom);
				}
			}
			//家庭成员记录
			for(int k=0;k<fname.length;k++){
				if(!"".equals(fname[k])&& null!=fname[k]){
					modelMember.setMebmer_income(fincom[k]);
					modelMember.setMember_birth_date(fbirth[k]);
					modelMember.setMember_job(fjob[k]);
					modelMember.setMember_name(fname[k]);
					modelMember.setMember_relation(relation[k]);
					modelMember.setMember_sex(fsex[k]);
					modelMember.setModel_id(iid);
					modelInfoService.addMember(modelMember);
				}
			}
			//
		}
		
		return goModelList();
	}

	//跳转：修改劳模页面
	public String goEditModel(){
		int mid=modelInfo.getModel_id();
		modelInfo=modelInfoService.queryModelById(mid);
		modelInfo.setIncomList(modelInfoService.queryIncomList(mid, 1));
		modelInfo.setBuzhuList(modelInfoService.queryIncomList(mid, 2));
		return "goEditModel";
	}

	//修改：修改劳模信息
	public String doEditModel(){
		int mid=modelInfo.getModel_id();
		ModelInfo modelInfo1=modelInfoService.queryModelById(mid);
		String old_image=modelInfo1.getModel_image();
		if(modelInfo.getFileupload()!=null){
			//删除旧图
			File file=null;
			file = new File(ServletActionContext.getServletContext().getRealPath("/")+old_image);
			file.delete();
			//添加新图
			String new_image=picfileupload(mid);
			modelInfo.setModel_image(new_image);
		}else{
			modelInfo.setModel_image(old_image);
		}
		
		//持久化
		modelInfoService.editModel(modelInfo);
		
			modelIncom=new ModelIncom();
			modelMember=new ModelMember();
			//删除原有记录
			modelInfoService.delIncomList(mid);
			modelInfoService.delMemberList(mid);
			//收入记录
			for(int i=0;i<incoms.length;i++){
				if(!"".equals(incomyears[i])&& null!=incoms[i]){
					modelIncom.setIncome_year(incomyears[i]);
					modelIncom.setIncome_sum(incoms[i]);
					modelIncom.setModel_id(mid);
					modelIncom.setIncome_type(1);
					modelInfoService.addIncom(modelIncom);
				}
			}
			//补助记录
			for(int j=0;j<buzhus.length;j++){
				if(!"".equals(buzhuyears[j])&& null!=buzhus[j]){
					modelIncom.setIncome_year(buzhuyears[j]);
					modelIncom.setIncome_sum(buzhus[j]);
					modelIncom.setModel_id(mid);
					modelIncom.setIncome_type(2);
					modelInfoService.addIncom(modelIncom);
				}
			}
			//家庭成员记录
			for(int k=0;k<fname.length;k++){
				if(!"".equals(fname[k])&& null!=fname[k]){
					modelMember.setMebmer_income(fincom[k]);
					modelMember.setMember_birth_date(fbirth[k]);
					modelMember.setMember_job(fjob[k]);
					modelMember.setMember_name(fname[k]);
					modelMember.setMember_relation(relation[k]);
					modelMember.setMember_sex(fsex[k]);
					modelMember.setModel_id(mid);
					modelInfoService.addMember(modelMember);
				}
			}
		
		return goModelList();
	}

	//删除：删除劳模信息
	public String doDelModel(){
		modelInfoService.delModel(modelInfo.getModel_id());
		return goModelList();
	}

	//跳转：劳模列表页面
	public String goModelList(){
		intPageSize = 10;
		lookForModel();
		setModelSesssion();
		return "goModelList";
	}
	//跳转：搜索按钮进入劳模列表
	public String goOption(){
		lookForModel();
		setModelSesssion();
		return "goModelList";
	}
	//跳转：翻页进入劳模列表
	public String goPage(){
		getModelSesssion();
		lookForModel();
		
		return "goModelList";
	}
	//查询劳模列表
	public void lookForModel(){
		if("".equals(sex))sex = 0;
		//if("".equals(honour))honour = null;
		if("".equals(level))level = null;
		if("".equals(current))current = null;
		if("".equals(intPageSize))intPageSize = 10;
		if("".equals(paramName))paramName = null;
		if("".equals(paramValue))paramValue = null;
		if("".equals(orderByName))orderByName = null;
		
		if(page<1){
			page = 1;
			intPage = 1;
			page = 1;
		}else{
			intPage = page;
		}
		intRowCount = modelInfoService.queryListCount(sex, null, level,
				current, paramName, paramValue);
		intPageCount = (intRowCount + intPageSize -1)/intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			modelList = modelInfoService.queryList(sex, null, level, 
					current, paramName, paramValue, orderByName, start, intPageSize);
	    }
	}
	//获取劳模列表查询条件
	public void getModelSesssion(){
		sex = (Integer)ServletActionContext.getRequest().getSession().getAttribute("sex");
		//honour = (String)ServletActionContext.getRequest().getSession().getAttribute("honour");
		level = (String)ServletActionContext.getRequest().getSession().getAttribute("level");
		current = (String)ServletActionContext.getRequest().getSession().getAttribute("current");
		paramName = (String)ServletActionContext.getRequest().getSession().getAttribute("paramName");
		paramValue = (String)ServletActionContext.getRequest().getSession().getAttribute("paramValue");
		orderByName = (String)ServletActionContext.getRequest().getSession().getAttribute("orderByName");
		intPageSize = (Integer)ServletActionContext.getRequest().getSession().getAttribute("intPageSize");
	}
	//写入劳模列表查询条件
	public void setModelSesssion(){
		ServletActionContext.getRequest().getSession().setAttribute("sex",sex); 
		//ServletActionContext.getRequest().getSession().setAttribute("honour",honour); 
		ServletActionContext.getRequest().getSession().setAttribute("level",level); 
		ServletActionContext.getRequest().getSession().setAttribute("current",current); 
		ServletActionContext.getRequest().getSession().setAttribute("paramName",paramName);
		ServletActionContext.getRequest().getSession().setAttribute("paramValue",paramValue); 
		ServletActionContext.getRequest().getSession().setAttribute("orderByName",orderByName); 
		ServletActionContext.getRequest().getSession().setAttribute("intPageSize",intPageSize); 
	}
	/**
	 * 图片文件上传
	 * @return boolean
	 */
	private String picfileupload(int model_id){
		String dstname = null;
		if(modelInfo.getFileupload()!=null){
    		try{
    	    	String sDirPath = ServletActionContext.getServletContext().getRealPath("/");
    	    	dstname = SAVEDIR + "/" +model_id+ generateFileName(modelInfo.getFileuploadFileName());
    		    File dstFile = new File(sDirPath + dstname);
    		    FileUtils.copyFile(modelInfo.getFileupload(), dstFile);
    		}catch(Exception e){
    			System.out.println(e);
    		}
    	}
        return dstname;
    }
	/**
	 * 自动分配文件名称
	 * @param fileName： yyMMddHHmmssXXXXX.ext
	 * @return String
	 */
    private String generateFileName(String fileName) {
        // 获得当前时间
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        // 转换为字符串
        String formatDate = format.format(new Date());
        // 随机生成文件编号
        int random = new Random().nextInt(10000);
        // 获得文件后缀名称
        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        // 组成一个新的文件名称
        return formatDate + random + extension;
    }
	public String getLOGIN_NAME() {
		return LOGIN_NAME;
	}
	public void setLOGIN_NAME(String lOGINNAME) {
		LOGIN_NAME = lOGINNAME;
	}
	public String getLOGIN_PWD() {
		return LOGIN_PWD;
	}
	public void setLOGIN_PWD(String lOGINPWD) {
		LOGIN_PWD = lOGINPWD;
	}
	public String getOperaResult() {
		return operaResult;
	}
	public void setOperaResult(String operaResult) {
		this.operaResult = operaResult;
	}
	public List<ModelInfo> getModelList() {
		return modelList;
	}
	public void setModelList(List<ModelInfo> modelList) {
		this.modelList = modelList;
	}
	public int getIntPageSize() {
		return intPageSize;
	}
	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	public String getCurrent() {
		return current;
	}
	public void setCurrent(String current) {
		this.current = current;
	}
	public String getParamName() {
		return paramName;
	}
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}
	public String getParamValue() {
		return paramValue;
	}
	public void setParamValue(String paramValue) {
		this.paramValue = paramValue;
	}
	public String getOrderByName() {
		return orderByName;
	}
	public void setOrderByName(String orderByName) {
		this.orderByName = orderByName;
	}
	public ModelInfo getModelInfo() {
		return modelInfo;
	}
	public void setModelInfo(ModelInfo modelInfo) {
		this.modelInfo = modelInfo;
	}
	public int[] getIds() {
		return ids;
	}
	public void setIds(int[] ids) {
		this.ids = ids;
	}
	public Float[] getIncoms() {
		return incoms;
	}
	public void setIncoms(Float[] incoms) {
		this.incoms = incoms;
	}
	public String[] getIncomyears() {
		return incomyears;
	}
	public void setIncomyears(String[] incomyears) {
		this.incomyears = incomyears;
	}
	public Float[] getBuzhus() {
		return buzhus;
	}
	public void setBuzhus(Float[] buzhus) {
		this.buzhus = buzhus;
	}
	public String[] getBuzhuyears() {
		return buzhuyears;
	}
	public void setBuzhuyears(String[] buzhuyears) {
		this.buzhuyears = buzhuyears;
	}
	public String[] getFname() {
		return fname;
	}
	public void setFname(String[] fname) {
		this.fname = fname;
	}
	public int[] getFsex() {
		return fsex;
	}
	public void setFsex(int[] fsex) {
		this.fsex = fsex;
	}

	public String[] getRelation() {
		return relation;
	}
	public void setRelation(String[] relation) {
		this.relation = relation;
	}
	public String[] getFjob() {
		return fjob;
	}
	public void setFjob(String[] fjob) {
		this.fjob = fjob;
	}
	public Float[] getFincom() {
		return fincom;
	}
	public void setFincom(Float[] fincom) {
		this.fincom = fincom;
	}
	public String[] getFbirth() {
		return fbirth;
	}
	public void setFbirth(String[] fbirth) {
		this.fbirth = fbirth;
	}
	public List<ModelBonus> getModelBonusList() {
		return modelBonusList;
	}
	public void setModelBonusList(List<ModelBonus> modelBonusList) {
		this.modelBonusList = modelBonusList;
	}
}
